/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.gt.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.constant.Constant;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.gt.entity.GtWorkReport;
import com.thinkgem.jeesite.modules.gt.service.GtWorkReportService;
import com.thinkgem.jeesite.modules.sys.entity.Area;
import com.thinkgem.jeesite.modules.sys.service.AreaService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 工作月报信息Controller
 * @author zad
 * @version 2018-03-26
 */
@Controller
@RequestMapping(value = "${frontPath}/gt/gtWorkReport")
public class GtWorkReportController extends BaseController {

	@Autowired
	private GtWorkReportService gtWorkReportService;
	@Autowired
	private AreaService areaService;
	
	@ModelAttribute
	public GtWorkReport get(@RequestParam(required=false) String id) {
		GtWorkReport entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = gtWorkReportService.get(id);
		}
		if (entity == null){
			entity = new GtWorkReport();
		}
		return entity;
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(GtWorkReport gtWorkReport,String menuId, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		this.areaService.fetchRegions(model);
		if(null == gtWorkReport.getArea() || StringUtils.isBlank(gtWorkReport.getArea().getCode()))
		{
			// 默认时间为当前时间
			//gtWorkReport.setMonth(new Date());
			gtWorkReport.setArea(UserUtils.getUser().getArea());
			
			// 工作月报查询时，不做限制  add by zad 2018-04-28
			/*Area area = UserUtils.getUser().getArea();
			String type = area.getType();
			// 省级或者地市，存放自己的组织机构
			if(Constant.Area.AREA_TYPE_REGION.equals(type) || Constant.Area.AREA_TYPE_PROVINCE.equals(type))
			{
				gtWorkReport.setArea(area);
			}
			// 县时，存放父级组织机构
			if(Constant.Area.AREA_TYPE_COUNTY.equals(type))
			{
				gtWorkReport.setArea(this.areaService.get(area.getParent().getId()));
			}*/
		}
		
		Page<GtWorkReport> page = gtWorkReportService.findPage(new Page<GtWorkReport>(request, response), gtWorkReport); 
		model.addAttribute("page", page);
		
		model.addAttribute("gtWorkReport", gtWorkReport);
		model.addAttribute("menuId", menuId);
		
		return "modules/gt/basic/gtWorkReportList";
	}

	@RequestMapping(value = "form")
	public String form(String oper,GtWorkReport gtWorkReport, Model model) {
		
		model.addAttribute("oper", oper);
		
		this.areaService.fetchRegions(model);
		
		model.addAttribute("gtWorkReport", gtWorkReport);
		return "modules/gt/basic/gtWorkReportForm";
	}

	@RequestMapping(value = "save")
	public String save(GtWorkReport gtWorkReport,String oper,HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, gtWorkReport)){
			return form(oper,gtWorkReport, model);
		}
		

		
		if(StringUtils.isBlank(gtWorkReport.getId()))
		{
			// 日志标题
			request.setAttribute("title", "添加工作月报:" + gtWorkReport.getName());
		}else
		{
			// 日志标题
			request.setAttribute("title", "修改工作月报:" + gtWorkReport.getName());
		}

		
		
		gtWorkReportService.save(gtWorkReport);
		addMessage(redirectAttributes, "保存工作月报信息成功");
		
		if("form".equals(oper))
		{
			return "redirect:"+super.frontPath+"/gt/gtWorkReport/form?menuId=" + gtWorkReport.getMenuId();
		}
		
		return "redirect:"+super.frontPath+"/gt/gtWorkReport?menuId=" + gtWorkReport.getMenuId();
	}
	
	@RequestMapping(value = "delete")
	public String delete(GtWorkReport gtWorkReport,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		gtWorkReportService.delete(gtWorkReport);
		addMessage(redirectAttributes, "删除工作月报信息成功");
		
		
		// 日志标题
		request.setAttribute("title", "删除工作月报:" + gtWorkReport.getId());
		
		
		return "redirect:"+super.frontPath+"/gt/gtWorkReport?menuId=" + gtWorkReport.getMenuId();
	}

}